<template>
	<el-form size="default">
		<el-form-item>
			<el-radio v-model="radioValue" :label="1">
				分钟，允许的通配符[, - * /]
			</el-radio>
		</el-form-item>

		<el-form-item>
			<el-radio v-model="radioValue" :label="2">
				周期从
				<el-input-number v-model="cycle01" :min="0" :max="58" />-
				<el-input-number
					v-model="cycle02"
					:min="cycle01 ? cycle01 + 1 : 1"
					:max="59"
				/>
				分钟
			</el-radio>
		</el-form-item>

		<el-form-item>
			<el-radio v-model="radioValue" :label="3">
				从
				<el-input-number v-model="average01" :min="0" :max="58" />
				分钟开始，每
				<el-input-number
					v-model="average02"
					:min="1"
					:max="59 - average01 || 0"
				/>
				分钟执行一次
			</el-radio>
		</el-form-item>

		<el-form-item>
			<el-radio v-model="radioValue" :label="4">
				指定
				<num-select
					v-model="checkboxList"
					:multiple="true"
					:start="0"
					:end="59"
					placeholder="可多选"
					label-format="minutes"
				></num-select>
			</el-radio>
		</el-form-item>
	</el-form>
</template>
<script lang="ts" setup>
import numSelect from "@/components/CronExpress/MultipleSelect/numSelect.vue";

interface Props {
	check: (value: number, min: number, max: number) => number;
	cron?: Record<string, any>;
}

const props = defineProps<Props>();
const emit = defineEmits<{
	(e: "update", field: "min", value: string, type: "min"): void;
}>();

// 响应式状态
const radioValue = ref(1);
const cycle01 = ref(1);
const cycle02 = ref(2);
const average01 = ref(0);
const average02 = ref(1);
const checkboxList = ref<number[]>([]);

// 计算属性
const cycleTotal = computed(() => {
	const c1 = props.check(cycle01.value, 0, 58);
	const c2 = props.check(cycle02.value, c1 ? c1 + 1 : 1, 59);
	return `${c1}-${c2}`;
});

const averageTotal = computed(() => {
	const a1 = props.check(average01.value, 0, 58);
	const a2 = props.check(average02.value, 1, 59 - a1 || 0);
	return `${a1}/${a2}`;
});

const checkboxString = computed(() =>
	checkboxList.value.length > 0 ? checkboxList.value.join(",") : "*"
);

// 监听变化
watch(radioValue, (newVal) => {
	switch (newVal) {
		case 1:
			emit("update", "min", "*", "min");
			break;
		case 2:
			emit("update", "min", cycleTotal.value, "min");
			break;
		case 3:
			emit("update", "min", averageTotal.value, "min");
			break;
		case 4:
			emit("update", "min", checkboxString.value, "min");
			break;
	}
});

watch(cycleTotal, (newVal) => {
	if (radioValue.value === 2) {
		emit("update", "min", newVal, "min");
	}
});

watch(averageTotal, (newVal) => {
	if (radioValue.value === 3) {
		emit("update", "min", newVal, "min");
	}
});

watch(checkboxString, (newVal) => {
	if (radioValue.value === 4) {
		emit("update", "min", newVal, "min");
	}
});
</script>
